package com.yyysj.common.core.domain;

import com.yyysj.common.core.enums.ResultCode;
import lombok.Data;

//@Data
//public class Result <T> {
//    private Integer code;
//    private String msg;
//    private T data;
//}

@Data
public class Result <T> {
    private Integer code;
    private String msg;
    private T data;

    public static <T> Result<T> build(int rows) {
        if(rows > 0) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    public static <T> Result<T> build(boolean flag) {
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    public static <T> Result<T> ok() {
        return assembleResult(ResultCode.SUCCESS, null);
    }

    public static <T> Result<T> ok(T data) {
        return assembleResult(ResultCode.SUCCESS, data);
    }

//    public static <T> Result<T> error() {
//        return assembleResult(ResultCode.ERROR,null);
//    }
//
//    public static <T> Result<T> error(int code, String msg, T data) {
//        return assembleResult(code,msg,data);
//    }
//
//    public static <T> Result<T> error(ResultCode resultCode) {
//        return assembleResult(resultCode,null);
//    }

    public static <T> Result<T> fail() {
        return assembleResult(ResultCode.FAILED,null);
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        return assembleResult(resultCode,null);
    }

    public static <T> Result<T> fail(int code, String msg) {
        return assembleResult(code,msg,null);
    }
    private static <T> Result<T> assembleResult(int code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    private static <T> Result<T> assembleResult(ResultCode resultCode, T data) {
        Result<T> result = new Result<>();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        result.setData(data);
        return result;
    }
}